﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq.Expressions;
using System.Reflection;
using System.Xml.Serialization;

namespace Services
{
    interface IDataAccess<T>
    {
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        int Save(T entity);
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        int Delete(object objValue);
        /// <summary>
        /// 是否存在
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        bool Exists(object objValue);

        /// <summary>
        /// 是否存在
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        bool Exists(Expression<Func<T, bool>> exp);
        /// <summary>
        /// 获得实体列表
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        List<T> GetList(Expression<Func<T, bool>> exp);
        /// <summary>
        /// 获得单个实体
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        T GetByID(object objValue);
    }


    public class ModelConvertHelper<T> where T : new()
    {
        /// <summary>
        /// DataTable转换成List
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static List<T> ConvertToModelList(DataTable dt)
        {

            List<T> ts = new List<T>();// 定义集合
            Type type = typeof(T); // 获得此模型的类型
            string tempName = "";
            foreach (DataRow dr in dt.Rows)
            {
                T t = new T();
                PropertyInfo[] propertys = t.GetType().GetProperties();// 获得此模型的公共属性
                foreach (PropertyInfo pi in propertys)
                {
                    tempName = pi.Name;
                    if (dt.Columns.Contains(tempName))
                    {
                        if (!pi.CanWrite) continue;
                        object value = dr[tempName];
                        if (value != DBNull.Value)
                        {
                            if (value.GetType() == typeof(UInt64))
                                pi.SetValue(t, (UInt64)value == 1 ? true : false, null);
                            else
                                pi.SetValue(t, value, null);
                        }
                    }
                }
                ts.Add(t);
            }
            return ts;
        }

        /// <summary>
        /// DataTable转换成单个实体
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static T ConvertToModel(DataTable dt)
        {
            T t = new T();
            string tempName = "";
            if (dt.Rows.Count < 1)
                return default(T);
            DataRow dr = dt.Rows[0];

            PropertyInfo[] propertys = t.GetType().GetProperties();// 获得此模型的公共属性
            foreach (PropertyInfo pi in propertys)
            {
                tempName = pi.Name;
                if (dt.Columns.Contains(tempName))
                {
                    if (!pi.CanWrite) continue;
                    object value = dr[tempName];
                    if (value != DBNull.Value)
                    {
                        if (value.GetType() == typeof(UInt64))
                            pi.SetValue(t, (UInt64)value == 1 ? true : false, null);
                        else
                            pi.SetValue(t, value, null);
                    }
                }
            }

            return t;
        }
    }

    public class Seriablizable
    {
        public static String Serialize(Type objType, Object objEntity)
        {
            String strRetVal = "";
            using (System.IO.StringWriter oWriter = new System.IO.StringWriter())
            {
                XmlSerializer oSerializer = new XmlSerializer(objType);
                oSerializer.Serialize(oWriter, objEntity);
                strRetVal = oWriter.ToString();
            }
            return strRetVal;
        }
        public static Object Deserialize(Type objType, String strEntity)
        {
            Object objRetVal = null;
            using (System.IO.StringReader oReader = new System.IO.StringReader(strEntity))
            {
                XmlSerializer oSerializer = new XmlSerializer(objType);
                objRetVal = oSerializer.Deserialize(oReader);
            }
            return objRetVal;
        }
    }

    public static class ExtensionMethods
    {
        #region 避免空引用
        /// <summary>
        /// 避免执行ToString()时抛出NullReferenceExeption
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string ToSafeString<T>(this T obj) where T : class
        {
            string result = null;
            if (obj != null)
            {
                result = obj.ToString();
            }
            return result;
        }

        /// <summary>
        /// 避免NullReferenceExeption
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static T Instance<T>(this T obj) where T : new()
        {
            if (obj == null)
            {
                obj = new T();
            }
            return obj;
        }
        #endregion

        #region 转换

        /// <summary>
        /// 返回Int类型值
        /// </summary>
        /// <param name="strValue"></param>
        /// <returns></returns>
        public static int ToInt(this string strValue)
        {
            return int.Parse(strValue);
        }
        public static Double ToDouble(this string strValue)
        {
            return Double.Parse(strValue);
        }
        public static Decimal ToDecimal(this string strValue)
        {
            return Decimal.Parse(strValue);
        }
        public static List<Guid> ToGuidArray(this string strValue)
        {
            List<Guid> list = new List<Guid>();
            foreach (var item in strValue.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
            {
                list.Add(Guid.Parse(item));
            }
            return list;
        }
        public static List<int> ToIntArray(this string strValue)
        {
            List<int> list = new List<int>();
            foreach (var item in strValue.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
            {
                list.Add(int.Parse(item));
            }
            return list;
        }
        #endregion

        #region 判断
        /// <summary>
        /// 判断是否是Int类型
        /// </summary>
        /// <param name="strValue"></param>
        /// <returns></returns>
        public static bool IsInt(this string strValue)
        {
            int temp = -1;
            return int.TryParse(strValue, out temp);
        }

        /// <summary>
        /// 判断是否是Double类型
        /// </summary>
        /// <param name="strValue"></param>
        /// <returns></returns>
        public static bool IsDouble(this string strValue)
        {
            Double temp = -1;
            return Double.TryParse(strValue, out temp);
        }

        /// <summary>
        /// 判断是否是Decimal类型
        /// </summary>
        /// <param name="strValue"></param>
        /// <returns></returns>
        public static bool IsDecimal(this string strValue)
        {
            Decimal temp = -1;
            return Decimal.TryParse(strValue, out temp);
        }
        #endregion

    }
}



